package dao.hibernate;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.MultiMap;
import org.apache.commons.collections.iterators.EntrySetMapIterator;
import org.hibernate.Query;

import util.Util;

import dao.ThietBiYTeDAO;
import model.ChuDe;
import model.LoaiThietBiYTe;
import model.ThietBiYTe;

public class ThietBiYTeDAOHibernate extends
		GenericHibernateDAO<ThietBiYTe, Long> implements ThietBiYTeDAO {

	@SuppressWarnings("unchecked")
	@Override
	public List<ThietBiYTe> findByTenChuDe(String name) {
		return getSession()
				.createQuery(
						"FROM ThietBiYTe AS thietBi WHERE thietBi.tenChuDe LIKE :pname")
				.setString("pname", "%" + name + "%").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ThietBiYTe> findByCongDung(String name) {
		return getSession()
				.createQuery(
						"FROM ThietBiYTe AS thietBi WHERE thietBi.congDung LIKE :pname")
				.setString("pname", "%" + name + "%").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ThietBiYTe> findByCachDung(String name) {
		return getSession()
				.createQuery(
						"FROM ThietBiYTe AS thietBi WHERE thietBi.cachDung LIKE :pname")
				.setString("pname", "%" + name + "%").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ThietBiYTe> findByNhaSanXuat(String name) {
		return getSession()
				.createQuery(
						"FROM ThietBiYTe AS thietBi WHERE thietBi.nhaSanXuat LIKE :pname")
				.setString("pname", "%" + name + "%").list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<ThietBiYTe> findByTenLoaiThietBiYTeLike(String keyword) {
		return getSession()
		.createQuery(
				"FROM ThietBiYTe AS thietBi WHERE thietBi.loaiThietBiYTe.tenLoaiThietBiYTe LIKE :pname")
		.setString("pname", "%" + keyword + "%").list();
	}

	public List<ThietBiYTe> findByOneCondition(String criteria, String keyWord) {
		List<ThietBiYTe> list = new ArrayList<ThietBiYTe>();
		if (criteria.equals("tenChuDe")) {
			list = findByTenChuDe(keyWord);
		} else if (criteria.equals("congDung")) {
			list = findByCongDung(keyWord);
		} else if (criteria.equals("cachDung")) {
			list = findByCachDung(keyWord);
		} else if (criteria.equals("nhaSanXuat")) {
			list = findByNhaSanXuat(keyWord);
		} else if (criteria.equals("tenLoaiThietBiYTe")) {
			list = findByTenLoaiThietBiYTeLike(keyWord);
		}
		
		List<ThietBiYTe> result = new ArrayList<ThietBiYTe>();
		ThietBiYTe thietBiYTeSimple = null;
		for (ThietBiYTe thietBi : list) {
			thietBiYTeSimple = new ThietBiYTe(thietBi.getLoaiThietBiYTe(), thietBi.getTenChuDe(), thietBi.getChuDeCha(), null, null, thietBi.getNhaSanXuat(), null);
			thietBiYTeSimple.setIdChuDe(thietBi.getIdChuDe());
			result.add(thietBiYTeSimple);
		}
		
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ThietBiYTe> findByKeyWord(String keyWord) {
		return getSession()
				.createQuery(
						"FROM ThietBiYTe AS thietBi WHERE thietBi.tenChuDe LIKE :pname OR "
								+ "thietBi.congDung LIKE :pname OR "
								+ "thietBi.cachDung LIKE :pname OR "
								+ "thietBi.nhaSanXuat LIKE :pname ")
				.setString("pname", "%" + keyWord + "%").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ThietBiYTe> findByManyConditions(MultiMap conditions) {
		List<ThietBiYTe> result = null;
		EntrySetMapIterator iterator = new EntrySetMapIterator(conditions);
		String criteria = "";
		List<String> keywords = new ArrayList<String>();
		String keyword = "";
		while (iterator.hasNext()) {
			criteria = (String) iterator.next();
			keywords = (ArrayList<String>) conditions.get(criteria);
			for (String k : keywords) {
				keyword = k;
				if (criteria.equals("tenThietBiYTe")) {
					List<ThietBiYTe> list = findByTenChuDe(keyword);
					result = Util.retainAllThietBiYTe(result, list);
				} else if (criteria.equals("congDung")) {
					List<ThietBiYTe> list = findByCongDung(keyword);
					result = Util.retainAllThietBiYTe(result, list);
				} else if (criteria.equals("cachDung")) {
					List<ThietBiYTe> list = findByCachDung(keyword);
					result = Util.retainAllThietBiYTe(result, list);
				} else if (criteria.equals("nhaSanXuat")) {
					List<ThietBiYTe> list = findByNhaSanXuat(keyword);
					result = Util.retainAllThietBiYTe(result, list);
				}
			}
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ThietBiYTe> findByIdLoaiThietBiYTe(long idLoaiThietBiYTe) {
		List<ThietBiYTe> list = new ArrayList<ThietBiYTe>();
		Query query = getSession().createQuery("SELECT thietBi.idChuDe, thietBi.tenChuDe, thietBi.chuDeCha, thietBi.loaiThietBiYTe, thietBi.nhaSanXuat FROM ThietBiYTe AS thietBi WHERE thietBi.loaiThietBiYTe.idLoaiThietBiYTe = :pname ORDER BY thietBi.tenChuDe")
		.setLong("pname", idLoaiThietBiYTe);
		Iterator iter = query.list().iterator();
		ThietBiYTe thietBi = null;
		while(iter.hasNext()){
			Object[] row = (Object[])iter.next();
			long idThietBi = (Long)row[0];
			String tenThietBi = (String)row[1];
			ChuDe chuDeCha = (ChuDe)row[2];
			LoaiThietBiYTe loaiThietBi = (LoaiThietBiYTe)row[3];
			String nhaSanXuat = (String)row[4];
			thietBi = new ThietBiYTe(loaiThietBi, tenThietBi, chuDeCha, null, null, nhaSanXuat, null);
			thietBi.setIdChuDe(idThietBi);
			list.add(thietBi);
		}
		return list;
	}
}
